package com.tyrant.macaulottery;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.flyco.banner.anim.select.ZoomInEnter;
import com.tyrant.macaulottery.adapter.BaseRecyclerAdapter;
import com.tyrant.macaulottery.adapter.BaseRecyclerViewHolder;
import com.tyrant.macaulottery.banner.BannerItem;
import com.tyrant.macaulottery.banner.SimpleImageBanner;
import com.tyrant.macaulottery.bean.BannerBean;
import com.tyrant.macaulottery.bean.MessageBean;
import com.tyrant.macaulottery.network.ApiLoader;
import com.tyrant.macaulottery.utils.BrowserHelper;
import com.tyrant.macaulottery.utils.ResultObserver;
import com.tyrant.macaulottery.utils.SharePreferencesUtil;
import com.tyrant.macaulottery.widget.FocusedTextView;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;


/**
 * A simple {@link Fragment} subclass.
 */
public class HomeFragment extends Fragment {


    @Bind(R.id.sib_the_most_comlex_usage)
    SimpleImageBanner simpleImageBanner;
    @Bind(R.id.ftv_message)
    FocusedTextView ftv_message;
    @Bind(R.id.swipe_target)
    RecyclerView swipe_target;
    @Bind(R.id.iv_bottom_1)
    ImageView iv_bottom_1;
    @Bind(R.id.iv_bottom_2)
    ImageView iv_bottom_2;
    @Bind(R.id.iv_bottom_3)
    ImageView iv_bottom_3;
    @Bind(R.id.iv_bottom_4)
    ImageView iv_bottom_4;
    private List<BannerItem> bannerItems = new ArrayList<>();
    ArrayList<BannerBean> mdata = new ArrayList<BannerBean>();
    BaseRecyclerAdapter adapter;
    List<BannerBean> localWebBottomList = new ArrayList<>();
    public HomeFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_home, container, false);
        ButterKnife.bind(this, view);
        initView();
        return view;

    }

    private void initView() {
        localWebBottomList.add(new BannerBean("http://103666.com","快速充值"));
        localWebBottomList.add(new BannerBean("http://hd67777.com","优惠申请"));
        localWebBottomList.add(new BannerBean("https://messenger.providesupport.com/messenger/0m1vo2kp98bcz12l2xymgn9kp2.html","在线客服"));
        localWebBottomList.add(new BannerBean("http://67777bb.com/Register","注册会员"));
        new ApiLoader().getBanner().subscribe(new ResultObserver<List<BannerBean>>() {
            @Override
            public void onNext(List<BannerBean> bannerBeen) {
                for (BannerBean bannerBean : bannerBeen) {
                    bannerItems.add(new BannerItem(bannerBean.getUrl(), "●" + bannerBean.getActivity(), 1, ""));
                }
                simpleImageBanner.setSelectAnimClass(ZoomInEnter.class)
                        .setSource(bannerItems)
                        .startScroll();
            }
        });
        new ApiLoader().getMessage().subscribe(new ResultObserver<List<MessageBean>>() {
            @Override
            public void onNext(List<MessageBean> messageBeens) {
                StringBuffer stringBuffer = new StringBuffer();
                for (MessageBean messageBeen : messageBeens) {
                    stringBuffer.append(messageBeen.getMessage());
                    stringBuffer.append(System.getProperty("line.separator"));
                }
                ftv_message.setText(stringBuffer.toString());
            }
        });

        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
        swipe_target.setLayoutManager(layoutManager);//设置显示方式


        adapter = new BaseRecyclerAdapter<BannerBean>(getActivity(), mdata, R.layout.home_webhost_item) {
            @Override
            public void init(BaseRecyclerViewHolder holder, BannerBean user, int position) {
                holder.setText(R.id.rtv_2, user.getUrl());
            }
        };
        adapter.setOnItemClickLinster(new BaseRecyclerAdapter.OnItemClickLinster() {
            @Override
            public void onItemClick(View view, int position) {
//                Toast.makeText(getActivity(), "mdata.get(position):" + mdata.get(position).getUrl(), Toast.LENGTH_SHORT).show();
                BrowserHelper.goBrowser(getActivity(), mdata.get(position).getUrl());
            }
        });
        swipe_target.setAdapter(adapter);//设置适配器

        new ApiLoader().getWebHost().subscribe(new ResultObserver<List<BannerBean>>() {
            @Override
            public void onNext(List<BannerBean> bannerBeens) {
                for (BannerBean bannerBeen : bannerBeens) {
                    mdata.add(bannerBeen);
                    adapter.notifyDataSetChanged();
                    SharePreferencesUtil.putSpString("registerWebHost", bannerBeen.getUrl(), getActivity());
                }
            }
        });
        new ApiLoader().getWebBottom().subscribe(new ResultObserver<List<BannerBean>>() {
            @Override
            public void onNext(List<BannerBean> webBottomList) {
                localWebBottomList.clear();
                localWebBottomList.addAll(webBottomList);
            }
        });
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @OnClick({R.id.iv_bottom_1, R.id.iv_bottom_2, R.id.iv_bottom_3, R.id.iv_bottom_4})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_bottom_1:
                BrowserHelper.goBrowser(getActivity(), localWebBottomList.get(0).getUrl());
                break;
            case R.id.iv_bottom_2:
                BrowserHelper.goBrowser(getActivity(), localWebBottomList.get(1).getUrl());
                break;
            case R.id.iv_bottom_3:
                BrowserHelper.goBrowser(getActivity(), localWebBottomList.get(2).getUrl());
                break;
            case R.id.iv_bottom_4:
                BrowserHelper.goBrowser(getActivity(), localWebBottomList.get(3).getUrl());
                break;
        }
    }
}
